﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using MSDNWebcast.Commands;
using MSDNWebcast.Models;
using MSDNWebcast.Service;

namespace MSDNWebcast.ViewModels
{
    /// <summary>
    /// 课程列表视图
    /// </summary>
    public class CoursesViewModel : WorkspaceViewModel
    {
        #region Fields

        /// <summary>
        /// 所有课程类别
        /// </summary>
        private List<CourseCatalog> m_Series;


        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<CourseCatalog> m_SeriesViewModels;

        /// <summary>
        /// 
        /// </summary>
        private CourseCatalog m_SelectedCourseCatalog;

        private ICommand m_Command;

        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<CourseViewModel> m_CourseViewModels;

        /// <summary>
        /// 
        /// </summary>
        private string m_ViewInfomation;

        /// <summary>
        /// 
        /// </summary>
        private string m_RightViewInfomation;

        /// <summary>
        /// 切换
        /// </summary>
        private ICommand m_SwithCommand;

        private bool m_EnableDownloadButton = true;

        #endregion

        #region Constructors

        #endregion

        #region Properties

        /// <summary>
        /// 搜索课程
        /// </summary>
        public ICommand SearchCommand
        {
            get
            {
                return m_Command ?? (m_Command = new DelegateCommand<string>(Search, CanSearch));
            }
        }

        /// <summary>
        /// 切换下载课程视图和所有课程视图
        /// </summary>
        public ICommand SwithCommand
        {
            get
            {
                return m_SwithCommand ?? (m_SwithCommand = new DelegateCommand(Switch, () => true));
            }
        }

        /// <summary>
        /// 左侧界面显示信息
        /// </summary>
        public string LeftViewInfomation
        {
            get { return m_ViewInfomation; }
            set
            {
                m_ViewInfomation = value;
                OnPropertyChanged("LeftViewInfomation");
            }
        }

        /// <summary>
        /// 右侧界面显示信息
        /// </summary>
        public string RightViewInfomation
        {
            get { return m_RightViewInfomation; }
            set
            {
                m_RightViewInfomation = value;
                OnPropertyChanged("RightViewInfomation");
            }
        }

        /// <summary>
        /// 仅显示下载的课程
        /// </summary>
        private bool ShowDownloadOnly
        {
            get { return !EnableDownloadButton; }
        }

        /// <summary>
        /// 选中的课程分类
        /// </summary>
        public CourseCatalog SelectedCourseCatalog
        {
            get
            {
                return m_SelectedCourseCatalog;
            }
            set
            {
                m_SelectedCourseCatalog = value;
                OnPropertyChanged("SelectedCourseCatalog");
                if (m_SelectedCourseCatalog != null)
                {
                    //Clean up
                    if (Courses != null)
                    {
                        foreach (var course in Courses)
                        {
                            course.CleanUp();
                        }
                    }

                    Courses = new ObservableCollection<CourseViewModel>();
                    foreach (var course in m_SelectedCourseCatalog.Courses)
                    {
                        if(ShowDownloadOnly)
                        {
                            if(course.HaveDownload())
                                Courses.Add(new CourseViewModel(course));
                        }
                        else
                        {
                            Courses.Add(new CourseViewModel(course));
                        }
                    }
                    RightViewInfomation = Courses.Count > 0 ? "" : "该课程系列无任何课程。";
                }
            }
        }


        /// <summary>
        /// 课程
        /// </summary>
        public ObservableCollection<CourseViewModel> Courses
        {

            get { return m_CourseViewModels; }
            private set
            {
                m_CourseViewModels = value;
                OnPropertyChanged("Courses");
            }
        }

        /// <summary>
        /// 系列课程
        /// </summary>
        public ObservableCollection<CourseCatalog> Series
        {
            
            get { return m_SeriesViewModels; }
            private set
            {
                m_SeriesViewModels = value;
                OnPropertyChanged("Series");
            }
        }

        /// <summary>
        /// 下载课程列表可用
        /// </summary>
        public bool EnableDownloadButton
        {
            get { return m_EnableDownloadButton; }
            set
            {
                m_EnableDownloadButton = value;
                OnPropertyChanged("EnableDownloadButton");
                OnPropertyChanged("EnableAllButton");
            }
        }

        /// <summary>
        /// 所有课程列表按钮可用
        /// </summary>
        public bool EnableAllButton
        {
            get { return !EnableDownloadButton; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string DisplayName
        {
            get { return "课程列表"; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 切换下载和所有课程
        /// </summary>
        public void Switch()
        {
            LeftViewInfomation = "";
            RightViewInfomation = "";
            if (EnableDownloadButton)
            {
                EnableDownloadButton = false;
                Series = new ObservableCollection<CourseCatalog>();
                foreach (var catalog in m_Series)
                {
                    if (catalog.HasDownload())
                    {
                        Series.Add(catalog);
                    }
                }
                if (Series.Count > 0)
                {
                    SelectedCourseCatalog = Series[0];
                }
                else
                {
                    SelectedCourseCatalog = null;
                    Courses = null;
                    LeftViewInfomation = "没有已下载网络广播！";
                    RightViewInfomation = "没有已下载网络广播！";
                }
            }
            else
            {
                EnableDownloadButton = true;
                Series = new ObservableCollection<CourseCatalog>(m_Series);
                SelectedCourseCatalog = Series[0];
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void Search(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                Series = new ObservableCollection<CourseCatalog>(m_Series);
                LeftViewInfomation = "";
            }
            else
            {
                text = text.ToLower();
                var result = new List<CourseCatalog>();
                foreach (var cata in m_Series)
                {
                    if (cata.Name.ToLower().Contains(text) || cata.Technology.ToLower().Contains(text))
                        result.Add(cata);
                }
                Series = new ObservableCollection<CourseCatalog>(result);
                LeftViewInfomation = Series.Count > 0 ? "" : string.Format("没有搜索到“{0}”相关的课程系列。", text);
            }
        }

        public bool CanSearch(string text)
        {
            return true;
        }

        /// <summary>
        /// 初始化视图
        /// </summary>
        public void InitialView()
        {
            m_Series = ServiceFactory.Instance.GetService<CourseService>().GetCourseSeries();
            Series = new ObservableCollection<CourseCatalog>(m_Series);

            SelectedCourseCatalog = Series[0];
        }

        #endregion
    }
}
